// @ts-ignore
import { isCmd, isKey, isCopy, isPaste, isUndo, isRedo, KEYS_COPY } from 'diagram-js/lib/features/keyboard/KeyboardUtil';
import { canOperate2 } from '../../utils';

const LOW_PRIORITY = 500;

const KEYS_SHEAR = ['x', 'X'];

const isShear = (event: any) => {
    return isCmd(event) && isKey(KEYS_SHEAR, event);
}

export default function KeyboardBindings(this: any, eventBus: any, keyboard: any, selection: any, modeling: any, customRenderer: any) {
    const self = this;
    eventBus.on('editorActions.init', LOW_PRIORITY, function (event: any) {
        const editorActions = event.editorActions;
        self.registerBindings(keyboard, editorActions, selection, modeling, customRenderer);
    });
};

KeyboardBindings.$inject = [
    'eventBus',
    'keyboard',
    'selection',
    'modeling',
    'customRenderer',
];

KeyboardBindings.prototype.registerBindings = function (keyboard: any, editorActions: any, selection: any, modeling: any, customRenderer: any) {
    const addListener = (action: any, fn: Function) => {
        if (editorActions.isRegistered(action)) {
            keyboard.addListener(fn);
        }
    };

    // 撤回
    // (CTRL|CMD) + Z
    addListener('undo', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (isUndo(event)) {
            editorActions.trigger('undo');
            return true;
        }
    });

    // 恢复
    // CTRL + Y
    // CMD + SHIFT + Z
    addListener('redo', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (isRedo(event)) {
            editorActions.trigger('redo');
            return true;
        }
    });

    // 复制
    // CTRL/CMD + C
    addListener('copy', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (isCopy(event)) {
            editorActions.trigger('copy');
            return true;
        }
    });

    // 粘贴
    // CTRL/CMD + V
    addListener('paste', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (isPaste(event)) {
            editorActions.trigger('paste');
            return true;
        }
    });

    // // zoom in one step
    // // CTRL/CMD + +
    // addListener('stepZoom', function (context: { keyEvent: any; }) {
    //     const event = context.keyEvent;
    //     if (isKey(['+', 'Add', '='], event) && isCmd(event)) {
    //         editorActions.trigger('stepZoom', { value: 1 });
    //         return true;
    //     }
    // });

    // // zoom out one step
    // // CTRL + -
    // addListener('stepZoom', function (context: { keyEvent: any; }) {
    //     const event = context.keyEvent;
    //     if (isKey(['-', 'Subtract'], event) && isCmd(event)) {
    //         editorActions.trigger('stepZoom', { value: -1 });
    //         return true;
    //     }
    // });

    // // zoom to the default level
    // // CTRL + 0
    // addListener('zoom', function (context: { keyEvent: any; }) {
    //     const event = context.keyEvent;
    //     if (isKey('0', event) && isCmd(event)) {
    //         editorActions.trigger('zoom', { value: 1 });
    //         return true;
    //     }
    // });

    // 删除元素
    // DEL
    addListener('removeSelection', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (isKey(['Backspace', 'Delete', 'Del'], event)) {
            const canDeleteSelection = selection?.get()?.filter(canOperate2);
            if (canDeleteSelection && canDeleteSelection?.length > 0) {
                modeling?.removeElements(canDeleteSelection);
                modeling?.setPropertiesPanelVisiable(false);
            }
            // editorActions.trigger('removeSelection');
            return true;
        }
    });

    // 剪切元素
    // SHEAR
    addListener('removeSelection', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (isShear(event)) {
            const canShearElement = selection?.get()?.filter(canOperate2);
            if (canShearElement && canShearElement?.length > 0) {
                const shearIds = canShearElement?.map((item: { id: string; }) => item?.id);
                customRenderer.shearCache = shearIds;
                editorActions.trigger('copy');
                modeling?.removeElements(canShearElement);
                modeling?.setPropertiesPanelVisiable(false);
                editorActions.trigger('paste');
                return true;
            }
        }
    });
};